package cn.rc.rcsystem.service.impl;

import cn.rc.rcsystem.domain.CarRental;
import cn.rc.rcsystem.domain.CarRentals;
import cn.rc.rcsystem.dto.CarRentalDto;
import cn.rc.rcsystem.exception.GlobalException;
import cn.rc.rcsystem.exception.GlobalExceptionCodeEnum;
import cn.rc.rcsystem.feign.LeaseService;
import cn.rc.rcsystem.mapper.CarRentalMapper;
import cn.rc.rcsystem.result.JSONResult;
import cn.rc.rcsystem.service.ICarRentalService;
import cn.rc.rcsystem.util.AssertUtil;
import cn.rc.rcsystem.util.BaiduAuditUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author director
 * @since 2022-12-09
 */
@Service
public class CarRentalServiceImpl extends ServiceImpl<CarRentalMapper, CarRental> implements ICarRentalService {
    @Autowired
    private LeaseService leaseService;
    /**
     * 店铺租赁车
     * @param carRental
     * @return
     */
    @Override
    public JSONResult rental(CarRental carRental) {
        AssertUtil.isNotNull(carRental, GlobalExceptionCodeEnum.COMMON_PARAM_IS_NULL_ERROR);
        AssertUtil.isTrue(BaiduAuditUtils.TextCensor(carRental.getCarNumber()), GlobalExceptionCodeEnum.BAIDUAI_ERROR);
        AssertUtil.isTrue(BaiduAuditUtils.TextCensor(carRental.getRemarks()), GlobalExceptionCodeEnum.BAIDUAI_ERROR);
        // 判断车辆是否存在
        EntityWrapper<CarRental> wrapper = new EntityWrapper<>();
        wrapper.eq("car_number",carRental.getCarNumber());
        CarRental carRental1 = selectOne(wrapper);
        AssertUtil.isNull(carRental1, GlobalExceptionCodeEnum.VEHICLE_ALREADY_EXISTS_ERROR);
        if(carRental.getCount()!=1){
            throw  new GlobalException(GlobalExceptionCodeEnum.PLEASE_ADD_THE_CORRECT_VEHICLE_NUMBER);
        }
        insert(carRental);
        return JSONResult.success();
    }

    @Override
    public CarRental charter(Long shopId, String carNumber) {
        //查询数据
        EntityWrapper<CarRental> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id",shopId);
        wrapper.eq("car_number",carNumber);
        return selectOne(wrapper);
    }

    /**
     * 车辆上架
     * @param carRentalDto
     * @return
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public JSONResult grounding(CarRentalDto carRentalDto) {
        String brand = carRentalDto.getBrand();
        BigDecimal deposit = carRentalDto.getDeposit();

        Long id = carRentalDto.getId();
        CarRental carRental1 = selectById(id);
        CarRentals carRentals = new CarRentals();
        BeanUtils.copyProperties(carRental1,carRentals);
        carRentals.setBrand(brand);
        carRentals.setDeposit(deposit);
        carRentals.setState(1);
        carRentals.setId(null);
        carRentals.setNumber(1);
        JSONResult cargrounding = leaseService.cargrounding(carRentals);
        if(!cargrounding.isSuccess()){
            String message = cargrounding.getMessage();
            throw  new GlobalException(message);
        }

        return JSONResult.success();
    }
}
